﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using ToolRental.Model;

namespace ToolRental.Services
{
    public class AdminService : IAdminService
    {

        #region GetToolTypes
        public List<ToolType> GetToolTypes()
        {
            var toolTypes = new List<ToolType>();
            using (var db = new MyDataContext())
            {
               toolTypes = db.ToolTypes.Select(p => new ToolType()
                {
                    BrandName = p.BrandName,
                    Id = p.Id,
                    TypeName = p.TypeName
                }).ToList();
            }

            return toolTypes;
        }
        #endregion

        #region GetTools
        public List<Tool> GetTools(int toolTypeId)
        {
            var tools = new List<Tool>();
            using (var db = new MyDataContext())
            {
                tools = db.Tools
                    .Where(p => p.ToolTypeId == toolTypeId)
                    .Select(p => new Tool()
                    {
                        Id = p.Id,
                        StockNumber = p.StockNumber,
                        ToolTypeId = p.ToolTypeId
                    }).ToList();
            }
            return tools;
        }
        #endregion

        #region AddToolType
        public void AddToolType(ToolType toolType)
        {
            using (var db = new MyDataContext())
            {
                var dbToolType = new Model.ToolType()
                    {
                        BrandName = toolType.BrandName,
                        TypeName = toolType.TypeName
                    };
                db.ToolTypes.Add(dbToolType);
                db.SaveChanges();
            }
        }
        #endregion

        #region AddTool
        public void AddTool(string stockNumber, int toolTypeId)
        {
            using (var db = new MyDataContext())
            {
                var dbToolType = db.ToolTypes.Find(toolTypeId);
                var dbTool = new Model.Tool()
                    {
                        StockNumber = stockNumber,
                        ToolTypeId = toolTypeId,
                        Type = dbToolType
                    };
                db.Tools.Add(dbTool);
                db.SaveChanges();
            }
        }
        #endregion

        #region NewOrder
        public void NewOrder(int idToolType, int idUser)
        {
            using (var db = new MyDataContext())
            {
                var dbToolType = db.ToolTypes.Find(idToolType);
                var dbUser = db.UserProfiles.Find(idUser);
                var newOrder = new Model.Order()
                    {
                        UserId = idUser,
                        ToolTypeId = idToolType,
                        StartDate = DateTime.Now,
                        User = dbUser,
                        Type = dbToolType
                    };
                db.Orders.Add(newOrder);
                db.SaveChanges();
            }
        }
        #endregion

        #region NewBorrow
        public void NewBorrow(int idOrder, int idTool, DateTime deadline)
        {
            using (var db = new MyDataContext())
            {
                var dbOrder = db.Orders.Find(idOrder);
                var idUser = dbOrder.UserId;
                var dbUser = db.UserProfiles.Find(idUser);
                var dbTool = db.Tools.Find(idTool);
                var newBorrow = new Model.Borrow()
                    {
                        Deadline = deadline,
                        UserId = idUser,
                        StartDate = DateTime.Now,
                        ToolId = idTool,
                        Tool = dbTool,
                        User = dbUser
                    };
                db.Borrows.Add(newBorrow);
                db.Orders.Remove(dbOrder);
                db.SaveChanges();
            }
        }
        #endregion

        #region GetOrders
        public List<Order> GetOrders()
        {
            var orders = new List<Order>();

            using (var db = new MyDataContext())
            {
                orders = db.Orders.Select(p => new Order()
                    {
                        Id = p.Id,
                        StartDate = p.StartDate,
                        UserId = p.UserId,
                        UserName = p.User.UserName,
                        BrandName = p.Type.BrandName,
                        ToolTypeId = p.ToolTypeId,
                        TypeName = p.Type.TypeName
                    }).ToList();
            }
            return orders;
        }
        #endregion

        #region GetBorrows
        public List<Borrow> GetBorrows()
        {
            var borrows = new List<Borrow>();
            using (var db = new MyDataContext())
            {
                borrows = db.Borrows.Select(p => new Borrow()
                    {
                        Deadline = p.Deadline,
                        Id = p.Id,
                        StartDate = p.StartDate,
                        ToolId = p.ToolId,
                        UserId = p.UserId,
                        BrandName = p.Tool.Type.BrandName,
                        TypeName = p.Tool.Type.TypeName,
                        UserName = p.User.UserName,
                        StockNumber = p.Tool.StockNumber
                    }).ToList();
            }
            return borrows;
        }
        #endregion

        #region GetOrderListElements
        public List<OrderListElement> GetOrderListElements(int userId)
        {
            var orderListElements = new List<OrderListElement>();
            using (var db = new MyDataContext())
            {
                orderListElements = db.ToolTypes.Select(p => new OrderListElement()
                {
                    BrandName = p.BrandName,
                    Id = p.Id,
                    TypeName = p.TypeName,
                    Availability = "Elérhető", // TODO Draft
                    Ordered = (db.Orders.Where(p2 => 
                        ((p2.ToolTypeId == p.Id)&&(p2.UserId == userId))).Count() > 0),
                    Borrowed = (db.Borrows.Where(p3 => 
                        ((p3.UserId == userId)&&(p3.Tool.Type.Id == p.Id))).Count() > 0)
                }).ToList();
            }

            return orderListElements;
        }
        #endregion

        #region GetAvailableTools
        public List<Tool> GetAvailableTools(int toolTypeid)
        {
            List<Tool> tools;
            using (var db = new MyDataContext())
            {
                tools = db.Tools
                    .Where(q => (q.ToolTypeId == toolTypeid))
                    .Where(p =>
                    (db.Borrows.Where(p2 => (p2.ToolId == p.Id)).Count() == 0))
                    .Select(p => new Tool()
                        {
                            Id = p.Id,
                            StockNumber = p.StockNumber,
                            ToolTypeId = p.ToolTypeId
                        }).ToList();
            }
            return tools;
        }
        #endregion

        #region DeleteBorrow
        public void DeleteBorrow(int borrowId)
        {
            using (var db = new MyDataContext())
            {
                var borrow = db.Borrows.Find(borrowId);
                db.Borrows.Remove(borrow);
                db.SaveChanges();
            }
        }
        #endregion
    }
}
